﻿//全局變量
var currentTab;
var previousTab; 
var tab_to_frame_mapping = new Array();
//初始化tab，在畫面進入時即call此方法
function InitTabs(){
	tab_to_frame_mapping[0] = "";
	for (var i=1; i<=8; i++){
		tab_to_frame_mapping[i]='t'+i+'Contents';
	}
	currentTab = document.getElementById('t1');
}
//打開一個標簽頁
//frameSrc：提交的標簽頁的地址
//frameName，標簽頁的名稱
function openTabs(frameSrc,frameName){
	//ONLY allow max 8 tabs to open
	var nextTabIndex = getDuplicateTab(frameName);
	if (nextTabIndex == -1){
		if(document.getElementById('t8').innerText!=""){
			alert('作業劃面已滿，請將滑鼠移至欲關閉視窗並點選滑鼠左鍵二下後即可');
			return;
		}
		nextTabIndex = getNextBlankTabIndex();
	}

	var nextTabId = 't'+nextTabIndex;
	//assign src to frame
	var nextTab = document.getElementById(nextTabId);
	var nextFrameId = tab_to_frame_mapping[nextTabIndex].replace(/Contents/,'Frames');
	var nextFrame = document.getElementById(nextFrameId);
	nextTab.innerText = frameName;
	nextFrame.src = frameSrc;
	previousTab = currentTab;
	//change style
	//invoke changeTabs()
	changeTab('t'+nextTabIndex);
}
//返回是否有重復的標簽頁，如果返回-1，則是沒有重復
function getDuplicateTab(tabText)
{
	for(var i=1;i<=8;i++){
		if(document.getElementById('t'+i).innerText==tabText){
			return parseInt(i);
		}
	}
	return -1;
}
//得到下一個空的tab
function getNextBlankTabIndex()
{
	for(var i=1;i<=8;i++){
		if(document.getElementById('t'+i).innerText==""){
			return parseInt(i);
		}
	}
}


//改變頁簽
function changeTabs() {
	//the clicked tab
	var selectedTab = window.event.srcElement;
	if (selectedTab.innerText == "") {
		return;
	}
	changeTab(selectedTab.id);
}
//switch current tab with the one specified by tabId
function changeTab(tabId){
	//To clear status message
	currentTab = document.getElementById(tabId);//update current selected tab
	for(var i=1; i<=8; i++){
	
		var tab = document.getElementById('t'+ i);
		var div = document.getElementById(tab_to_frame_mapping[i]);
		
		if(tab.innerText == ""){//the loop will break until the last tab
			break;
		}
		
		if(tab == currentTab)//tab is selected tab
		{
			div.className='conts1';//visible
			//if(i<=3){//For 3 fixed tab
			//	tab.className = 'selTab3';
			//}
			//else{
				tab.className='selTab';
			//}
		}
		else
		{
			div.className='conts';//invisible
			//if(i<=3){//For 3 fixed tab
			//	tab.className = 'tab3';
			//}
			//else{
				tab.className='tab';
			//	}
		}
	}
	//print_tab_to_frame_mapping("");
}
//按名稱選中頁簽
function changeTabbyName(name){
	var id = parent.getDuplicateTab(name);
	if(id ==  -1){
		return -1;
	}
	parent.changeTab('t'+id);
}

function closeTabs()
{
	//as double click event will invoke onclick event firstly
	//so by the time running to this line, currentTab is already switched to selectedTab
	var selectedTab = currentTab;
	var selectTabIndex = parseInt(selectedTab.id.substring(1));
	//double click on the blank, dismiss it.
	if(selectedTab.innerText == ""){
		return;
	}
	//double click on the first 3 tabs, dismiss it
	//if(selectTabIndex <=3){
		//return;
	//}
	closeTab(selectedTab.id, 0);
	
}
//
function closeTabByName(tabText){
	var iTab;
	for(var i=1;i<=8;i++){
		if(document.getElementById('t'+i).innerText==tabText){
			iTab = parseInt(i);
		}
	}
	closeTab(iTab,0);
}

//close the tab specified by tabId and switch to the tab specified by aTabId
//aTabId = 0 indicate to switch to the the one in front of deleted tab
//switch the selection to the one before
function closeTab(tabId, aTabId)
{
	var selectTabIndex = parseInt(tabId.substring(1));
	var selectedTab = document.getElementById(tabId);
	var selectDiv = document.getElementById(tab_to_frame_mapping[selectTabIndex]);
	var selectFrame = document.getElementById(tab_to_frame_mapping[selectTabIndex].replace(/Contents/,'Frames'));

	//print_tab_to_frame_mapping("closeTab before");
	//selectDiv.className='conts';
	//last tab, close it, no need to update tab_to_frame_mapping
	//otherwise, need to update tab_to_frame_mapping
	
	selectedTab.className = "tabInvisible";
	selectFrame.src = "";
	selectedTab.innerText = "";
	selectDiv.className = "conts";

	var toTabId;
	
	if(!isLastTab(selectTabIndex)){
		leftMoveTab(selectedTab.id);
		toTabId = 't'+(getNextBlankTabIndex()-1);
		//changeTab('t'+(getNextBlankTabIndex()-1));
	}
	else{
		toTabId = 't'+ (selectTabIndex-1);
		//changeTab('t'+ (selectTabIndex-1));
	}
	if (aTabId!=0){
		toTabId = aTabId;
	}
	
	changeTab(toTabId);
}

function leftMoveTab(selectedTabId)
{
	var selectedTabIndex = parseInt(selectedTabId.substring(1));
	var selectedDivId = tab_to_frame_mapping[selectedTabIndex];
	var selectedDiv = document.getElementById(tab_to_frame_mapping[selectedTabIndex]);
	var selectedTabText = document.getElementById(selectedTabId).innerText;
	var selectedTabClass = document.getElementById(selectedTabId).className;
	var selectedDivClass = selectedDiv.className;
	
	var j=selectedTabIndex+1;
	for(var i=selectedTabIndex; i<=7; i++){
		//print_tab_to_frame_mapping("start move from " + j + "to " + i);
		tab_to_frame_mapping[i] = tab_to_frame_mapping[j];
		//tab text
		document.getElementById('t' + i).innerText = document.getElementById('t'+j).innerText;
		//tab class
		document.getElementById('t' + i).className = document.getElementById('t'+j).className;
		//div class
		document.getElementById(tab_to_frame_mapping[i]).className = document.getElementById(tab_to_frame_mapping[j]).className;
		
		j++;

	}

	tab_to_frame_mapping[8]= selectedDivId;
	document.getElementById('t8').innerText = selectedTabText;
	document.getElementById('t8').className = selectedTabClass;
	document.getElementById(tab_to_frame_mapping[8]).className = selectedDivClass;
	
}
//check if the selected tab is the last one
function isLastTab(tabIndex)
{
	return (tabIndex == 8 || getNextBlankTabIndex()==tabIndex+1);
}